package com.wwtech.epp.ui.activity.work.presenter;

import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;

import com.socks.library.KLog;
import com.wwtech.epp.contain.ContainValue;
import com.wwtech.epp.data.api.HttpAPIWrapper;
import com.wwtech.epp.db.Friend;
import com.wwtech.epp.entity.BaseBack;
import com.wwtech.epp.entity.ProjectEntity;
import com.wwtech.epp.ui.activity.work.NewProjectActivity;
import com.wwtech.epp.ui.activity.work.ProjectReportActivity;
import com.wwtech.epp.ui.activity.work.contract.ProjectReportContract;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

import static com.wwtech.epp.ui.activity.work.NewProjectActivity.RESULTOK;

/**
 * @author hzp
 * @Package com.wwtech.epp.ui.activity.work
 * @Description: presenter of ProjectReportActivity
 * @date 2017/10/04 10:03:43
 */
public class ProjectReportPresenter implements ProjectReportContract.ProjectReportContractPresenter{

    HttpAPIWrapper httpAPIWrapper;
    private final ProjectReportContract.View mView;
    private CompositeDisposable mCompositeDisposable;
    private ProjectReportActivity mActivity;

    private ArrayList<Friend> partPerson = new ArrayList<>();              //项目的参与人的集合
    private Friend tempFriend;

    @Inject
    public ProjectReportPresenter(@NonNull HttpAPIWrapper httpAPIWrapper, @NonNull ProjectReportContract.View view, ProjectReportActivity activity) {
        mView = view;
        this.httpAPIWrapper = httpAPIWrapper;
        mCompositeDisposable = new CompositeDisposable();
        this.mActivity = activity;
    }
    @Override
    public void subscribe() {

    }

    @Override
    public void init() {
        partPerson = new ArrayList<>();
        tempFriend = new Friend("", "-1", "", Uri.parse(""));
        tempFriend.setUserId("-1");
        partPerson.add(tempFriend);
        mView.setPersonData(partPerson);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == NewProjectActivity.CHECKPART && resultCode == RESULTOK) {
            partPerson = ContainValue.partPerson;
            mView.setPersonData(partPerson);
        }
    }

    @Override
    public void getProjectListFromServer() {
        Map<String, String> map = new HashMap<>();
        map.put("pageSize", "100");
        map.put("pageNum", "1");
        Disposable disposable = httpAPIWrapper.getProjectOwn(ContainValue.userLogin.getData().getUserId() + "", map)
                .subscribe(new Consumer<ProjectEntity>() {
                    @Override
                    public void accept(ProjectEntity projectEntity) throws Exception {
                        //isSuccesse
                        KLog.i("onSuccesse");
                        mView.setListData(projectEntity);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        //onError
                        KLog.i("onError");
                        mView.closeProgressDialog();
                        throwable.printStackTrace();
                    }
                }, new Action() {
                    @Override
                    public void run() throws Exception {
                        //onComplete
                        KLog.i("onComplete");
                    }
                });
        mCompositeDisposable.add(disposable);
    }

    @Override
    public void report(String projectId, Map map) {
        Disposable disposable = httpAPIWrapper.report(projectId, map)
                .subscribe(new Consumer<BaseBack>() {
                    @Override
                    public void accept(BaseBack projectEntity) throws Exception {
                        //isSuccesse
                        KLog.i("onSuccesse");
                        mView.reportSuccess();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        //onError
                        KLog.i("onError");
                        mView.closeProgressDialog();
                        throwable.printStackTrace();
                    }
                }, new Action() {
                    @Override
                    public void run() throws Exception {
                        //onComplete
                        KLog.i("onComplete");
                    }
                });
        mCompositeDisposable.add(disposable);
    }

    @Override
    public void unsubscribe() {
        if (!mCompositeDisposable.isDisposed()) {
             mCompositeDisposable.dispose();
        }
    }

//    @Override
//    public void getUser(HashMap map) {
//        //mView.showProgressDialog();
//        Disposable disposable = httpAPIWrapper.getUser(map)
//                .subscribe(new Consumer<User>() {
//                    @Override
//                    public void accept(User user) throws Exception {
//                        //isSuccesse
//                        KLog.i("onSuccesse");
//                        mView.setText(user);
//                      //mView.closeProgressDialog();
//                    }
//                }, new Consumer<Throwable>() {
//                    @Override
//                    public void accept(Throwable throwable) throws Exception {
//                        //onError
//                        KLog.i("onError");
//                        throwable.printStackTrace();
//                      //mView.closeProgressDialog();
//                      //ToastUtil.show(mActivity, mActivity.getString(R.string.loading_failed_1));
//                    }
//                }, new Action() {
//                    @Override
//                    public void run() throws Exception {
//                        //onComplete
//                        KLog.i("onComplete");
//                    }
//                });
//        mCompositeDisposable.add(disposable);
//    }
}